/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: AliceMana
 * Date: 2023-01-02
 * Time: 17:18
 */

    /*有三个线程，分别只能打印A，B和C
    要求按顺序打印ABC，打印10次
    输出示例：
    ABC
    ABC
    ABC
    ABC
    ABC
    ABC
    ABC
    ABC
    ABC
    ABC*/
public class Test {
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            Object locker1 = new Object();
            Object locker2 = new Object();

            Thread t1 = new Thread(() -> {
                System.out.print("A");
                synchronized (locker1) {
                    locker1.notify();
                }
            });
            Thread t2 = new Thread(() -> {
                synchronized (locker1) {
                    try {
                        locker1.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.print("B");
                synchronized (locker2) {
                    locker2.notify();
                }
            });
            Thread t3 = new Thread(() -> {
                synchronized (locker2) {
                    try {
                        locker2.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.print("C");
            });


            t2.start();
            t3.start();
            Thread.sleep(500);
            t1.start();
            System.out.println();
        }
    }
}
